from rest_framework.generics import CreateAPIView, RetrieveUpdateAPIView
from rest_framework.mixins import CreateModelMixin, ListModelMixin
from rest_framework.permissions import IsAuthenticated
from rest_framework.response import Response
from rest_framework.views import APIView
from rest_framework.viewsets import GenericViewSet

from .models import User
from users import serializers


# /user/
class UserDetailView(RetrieveUpdateAPIView):
    """用户基本信息"""
    serializer_class = serializers.UserDetailSerializer
    permission_classes = [IsAuthenticated]  # 指明必须登录认证后才能访问

    def get_object(self):
        # 返回当前请求的用户
        # 在类视图对象中，可以通过类视图对象的属性获取request
        # 在django的请求request对象中，user属性表明当请请求的用户
        return self.request.user

    def put(self, request, *args, **kwargs):
        user = self.get_object()
        serializer = serializers.UpdateUserDetailSerializer(instance=user, data=request.data)
        serializer.is_valid(raise_exception=True)
        serializer.save()
        return Response(serializer.data)


# url(r'^usernames/(?P<username>\w{5,20})/count/$', views.UsernameCountView.as_view()),
class UsernameCountView(APIView):
    """
    用户名数量
    """

    def get(self, request, username):
        """
        获取指定用户名数量
        """
        count = User.objects.filter(username=username).count()

        data = {
            'username': username,
            'count': count
        }

        return Response(data)


# url(r'^mobiles/(?P<mobile>1[3-9]\d{9})/count/$', views.MobileCountView.as_view()),
class MobileCountView(APIView):
    """
    手机号数量
    """

    def get(self, request, mobile):
        """
        获取指定手机号数量
        """
        count = User.objects.filter(mobile=mobile).count()

        data = {
            'mobile': mobile,
            'count': count
        }

        return Response(data)


# url(r'^users/$', views.UserView.as_view()),
class UserView(CreateAPIView):
    """
    用户注册
    """
    serializer_class = serializers.CreateUserSerializer


class UserWorkExperienceViewSet(RetrieveUpdateAPIView, CreateModelMixin, ListModelMixin, GenericViewSet):
    serializer_class = serializers.UserWorkExperienceSerializer
    permissions = [IsAuthenticated]

    def get_queryset(self):
        return self.request.user.work_experiences

    def list(self, request, *args, **kwargs):
        """用户工作经历列表视图"""
        queryset = self.get_queryset()
        serializer = self.get_serializer(queryset, many=True)
        user = self.request.user
        return Response({
            'user_id': user.id,
            'work_experience': serializer.data
        })

    def create(self, request, *args, **kwargs):
        """添加用户工作经历视图"""
        return super().create(request, *args, **kwargs)

    def update(self, request, *args, **kwargs):
        """更新用户工作经历视图"""
        return super().update(request, *args, **kwargs)


class UserEducationExperienceViewSet(RetrieveUpdateAPIView, CreateModelMixin, ListModelMixin, GenericViewSet):
    serializer_class = serializers.UserEducationExperienceSerializer
    permissions = [IsAuthenticated]

    def get_queryset(self):
        return self.request.user.education_experiences

    def list(self, request, *args, **kwargs):
        """用户教育经历列表视图"""
        queryset = self.get_queryset()
        serializer = self.get_serializer(queryset, many=True)
        user = self.request.user
        return Response({
            'user_id': user.id,
            'education_experience': serializer.data
        })

    def create(self, request, *args, **kwargs):
        """添加用户教育经历视图"""
        return super().create(request, *args, **kwargs)

    def update(self, request, *args, **kwargs):
        """修改用户教育经历视图"""
        return super().update(request, *args, **kwargs)
